package clientView;

import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusAdapter;
import java.awt.event.FocusEvent;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.event.CaretEvent;
import javax.swing.event.CaretListener;

import object.Position;
import utility.StringUtil;
import clientMain.Main;
import clientUtilGUI.UtilityGUI;
import clientView.Pos_View.Mouse_tblPos;
import database.PositionDelegate;

public class Pos_Insert extends JDialog implements ActionListener{

	private JTextField txtPosTName;
	private JLabel lblPosTName;
	private JLabel lblPosTDes;
	private JTextArea txaEmTDes;
	private JScrollPane scrollPane;
	private JButton btnSave;
	private JButton btnCancel;

	/**
	 * Create the frame.
	 */
	public Pos_Insert(JFrame main)  {
		super(main, "Thêm Chức Vụ", true);
		setDefaultCloseOperation(DISPOSE_ON_CLOSE);
		int height = 200;
	    int width = 420;
	    setSize(width, height);
	    setLocation((UtilityGUI.screenSize.width-width)/2, (UtilityGUI.screenSize.height-height)/2);
		setResizable(false);
	    initComponet();
	}
		
		public void initComponet()
		{	
		GridBagLayout gridBagLayout = new GridBagLayout();
		gridBagLayout.columnWidths = new int[]{0, 0, 0};
		gridBagLayout.rowHeights = new int[]{0, 0, 0, 0};
		gridBagLayout.columnWeights = new double[]{0.0, 0.5, 0.0};
		gridBagLayout.rowWeights = new double[]{0.0, 0.0, 0.0, 1.0};
		getContentPane().setLayout(gridBagLayout);
		
		lblPosTName = new JLabel("Chức Vụ: ");
		lblPosTName.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTName = new GridBagConstraints();
		gbc_lblEmTName.anchor = GridBagConstraints.WEST;
		gbc_lblEmTName.insets = new Insets(15, 15, 5, 5);
		gbc_lblEmTName.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTName.gridx = 0;
		gbc_lblEmTName.gridy = 0;
		getContentPane().add(lblPosTName, gbc_lblEmTName);
		
		txtPosTName = new JTextField();
		GridBagConstraints gbc_textField = new GridBagConstraints();
		gbc_textField.gridwidth = 2;
		gbc_textField.insets = new Insets(15, 5, 5, 15);
		gbc_textField.fill = GridBagConstraints.HORIZONTAL;
		gbc_textField.gridx = 1;
		gbc_textField.gridy = 0;
		getContentPane().add(txtPosTName, gbc_textField);
		txtPosTName.setColumns(10);
		txtPosTName.addFocusListener(new FocusAdapter() {
			public void focusLost(FocusEvent e){
				txtPosTName.setText(StringUtil.removeUnwantedSpace(txtPosTName.getText()));
			}
		});
		//check database 
		txtPosTName.addCaretListener(new CaretListener() {
			public void caretUpdate(CaretEvent arg0) {
				PositionDelegate d = new PositionDelegate();
				if(!d.CheckInsertPosName((StringUtil.removeUnwantedSpace(txtPosTName.getText())))&&txtPosTName.getText().length()>3){
					btnSave.setEnabled(true);
				}else{
					btnSave.setEnabled(false);
					
				}
			
			}
		});
		lblPosTDes = new JLabel("Mô tả: ");
		lblPosTDes.setFont(UtilityGUI.fontLblForm);
		GridBagConstraints gbc_lblEmTDes = new GridBagConstraints();
		gbc_lblEmTDes.anchor = GridBagConstraints.WEST;
		gbc_lblEmTDes.insets = new Insets(5, 15, 5, 5);
		gbc_lblEmTDes.fill = GridBagConstraints.VERTICAL;
		gbc_lblEmTDes.gridx = 0;
		gbc_lblEmTDes.gridy = 1;
		getContentPane().add(lblPosTDes, gbc_lblEmTDes);
		
		scrollPane = new JScrollPane();
		txaEmTDes = new JTextArea();
		txaEmTDes.setRows(4);
		GridBagConstraints gbc_scrollPane = new GridBagConstraints();
		gbc_scrollPane.gridwidth = 2;
		gbc_scrollPane.insets = new Insets(5, 5, 5, 15);
		gbc_scrollPane.fill = GridBagConstraints.BOTH;
		gbc_scrollPane.gridx = 1;
		gbc_scrollPane.gridy = 1;
		scrollPane.setViewportView(txaEmTDes);
		getContentPane().add(scrollPane, gbc_scrollPane);
		
		btnSave = new JButton("Lưu");
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnSave.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnSave = new GridBagConstraints();
		gbc_btnSave.fill = GridBagConstraints.VERTICAL;
		gbc_btnSave.anchor = GridBagConstraints.EAST;
		gbc_btnSave.insets = new Insets(10, 0, 5, 5);
		gbc_btnSave.gridx = 1;
		gbc_btnSave.gridy = 2;
		getContentPane().add(btnSave, gbc_btnSave);
		btnSave.addActionListener(this);
		
		btnCancel = new JButton("Thoát");
		btnSave.setCursor(UtilityGUI.cursorBtn);
		btnCancel.setFont(UtilityGUI.fontBtnForm);
		GridBagConstraints gbc_btnCancel = new GridBagConstraints();
		gbc_btnCancel.fill = GridBagConstraints.BOTH;
		gbc_btnCancel.insets = new Insets(10, 0, 5, 15);
		gbc_btnCancel.gridx = 2;
		gbc_btnCancel.gridy = 2;
		getContentPane().add(btnCancel, gbc_btnCancel);
		btnCancel.addActionListener(this);
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if(e.getSource() == btnSave){
			if(!StringUtil.isValidName(txtPosTName.getText(),200,2)){
				JOptionPane.showMessageDialog(null, "Tên Chức Vụ Ko để trống", 
						UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
				return;
			}else{
				Position pos = new Position();
				pos.setPosName(StringUtil.removeUnwantedSpace(txtPosTName.getText()));
				pos.setPosDes(StringUtil.removeUnwantedSpace(txaEmTDes.getText()));
				PositionDelegate posDelegate = new PositionDelegate();
				boolean flag = posDelegate.insertPos(pos, Main.userId);
				if(flag == true){
					this.setVisible(false);
					JOptionPane.showMessageDialog(null, UtilityGUI.successAction, 
							UtilityGUI.titleAlert, JOptionPane.INFORMATION_MESSAGE);
					Pos_View.tblPos.setVisible(false);
					Pos_View.tblPos = UtilityGUI.getTable("Position", 1, "Pos_Id", true);
					Pos_View.tblPos.addMouseListener(new Mouse_tblPos());
					Pos_View.scrollPane.setViewportView(Pos_View.tblPos);
					dispose();
				}else{
					JOptionPane.showMessageDialog(null, UtilityGUI.errorAction, 
							UtilityGUI.titleAlert, JOptionPane.ERROR_MESSAGE);
					return;
				}
			}
		}else if(e.getSource() == btnCancel){
			dispose();
		}
		
	}

}
